#include"comm.hpp"

int main()
{
    // 1.通过路径得到唯一的k值
    key_t k = ftok(PATH_NAME, PERMISSIONS);
    assert(k != -1);

    // 2.获取共享内存
    int shmid = shmget(k, SIZE, 0);
    assert(shmid != -1);
    Log("获取共享内存成功", Debug) << endl;
    // 3.打开共享内存
    char *shmaddr = (char *)shmat(shmid, nullptr, 0);
    Log("打开共享内存成功", Debug) << endl;
    //开始通信
        int fd = openFIFO(FIFO_NAME, O_WRONLY);
    while(true)
    {
        ssize_t s = read(0, shmaddr, SIZE);
        if(s>0)
        {
            shmaddr[s-1] = 0;
            Signal(fd);
            if(strcmp(shmaddr,"quit") == 0) break;
        }
    }
    CloseFD(fd);
    // 4.指定共享内存从地址空间去除挂接
    int n = shmdt(shmaddr);
    assert(n != -1);
    Log("去挂接共享内存成功",Debug) << endl;
    return 0;
}